# -*- coding: utf-8 -*-
# @Time     : 2021/4/9 17:14
# @Author   : pengrenjie
# @Email    : 941257162@.com
# @File     : 文件操作练习.py.py
import random
import collections
with open('ips.txt','a+') as fp:

    for j in range(120000):

        i = random.randint(1, 255)
        ranip = '172.25.254.' + f'{i}'
        fp.write(f"{ranip}\n")
    ips_dict = dict()
    fp.seek(0)
    for ip in fp:
        # 去除'\n'
        ip = ip.strip()

        if ip in ips_dict:

            ips_dict[ip] += 1
        else:
            ips_dict[ip] = 1
            # 按ip次数降序排序
    sorted_ip = sorted(ips_dict.items(), key=lambda ips_dict_: ips_dict_[1], reverse=True)

        # 输出频率前十的ip
    print(sorted_ip[0:10])
